﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.ComponentModel;
using System.Globalization;
using System.Web.Configuration;

namespace HospitalSystem
{
    public partial class Physical : System.Web.UI.Page
    {
        public static List<string> x = new List<string>();
        public static List<string> y = new List<string>();
        DataTable dt = new DataTable();
        DataTable ds = new DataTable();
        DataTable dx = new DataTable();
        DataTable dy = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {

         
            if (!IsPostBack)
            {
                BindData();
                BindData2();
       
            }
        }

        protected void BindData()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);

            x.Clear();
            scon.Open();
            SqlCommand cmd = new SqlCommand("select Name from PhysicalProblem", scon);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read() != false)
            {
                x.Add(reader["Name"].ToString());
            }
            reader.Close();
            dx.Columns.Add("Name");

            for (int i = 0; i < x.Count(); i++)
            {
                dx.Rows.Add();
                dx.Rows[i]["Name"] = x[i].ToString();
            }
            GridView1.DataSource = dx;
            GridView1.DataBind();
            scon.Close();

            //BindDataread();
        }
        protected void BindData2()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            y.Clear();
            scon.Open();
            SqlCommand cmd = new SqlCommand("select Name from SelfCareAbility", scon);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read() != false)
            {
                y.Add(reader["Name"].ToString());
            }
            reader.Close();
            dy.Columns.Add("Name");

            for (int i = 0; i < y.Count(); i++)
            {
                dy.Rows.Add();
                dy.Rows[i]["Name"] = y[i].ToString();
            }
            GridView2.DataSource = dy;
            GridView2.DataBind();
            scon.Close();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            PhysicalEval();
            SelfCareEval();
        }
        protected void PhysicalEval()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);


            if (dt.Columns.Count <= 0)
            {
                dt.Columns.Add("CID");
                dt.Columns.Add("CaseNo");
                dt.Columns.Add("PhysicalEvalCode");
                dt.Columns.Add("PhysicalEvalValue");
                dt.Columns.Add("RecorderID");
                dt.Columns.Add("EvalDateTime");
                dt.Columns.Add("CMPlanNo");
            }
            foreach (GridViewRow row in GridView1.Rows)
            {

                scon.Open();
                SqlCommand cmd = new SqlCommand("select PhysicalEvalCode from PhysicalProblem where Name = @sj", scon);
                cmd.Parameters.AddWithValue("@sj", GridView1.Rows[row.RowIndex].Cells[0].Text);
                SqlDataReader reader2 = cmd.ExecuteReader();
                RadioButtonList RBLP = (RadioButtonList)row.FindControl("RBL");
                if (reader2.Read() != false)
                {
                    dt.Rows.Add(Session["CID"], Session["CaseNo"], reader2["PhysicalEvalCode"].ToString(), RBLP.SelectedValue.ToString(), Session["Staff_id"], DateTime.Now.ToString(), Session["CMPlanNo"]);
                }
                else
                {

                }
                scon.Close();
            }
            //GridView3.DataSource = dt;
            //GridView3.DataBind();


            SqlCommand command2 = new SqlCommand("delete from [PhysicalEvaluation] where CID=@CID and CaseNo=@CaseNo", scon);
            scon.Open();
            command2.Parameters.AddWithValue("@CID", Session["CID"]);
            command2.Parameters.AddWithValue("@CaseNo", Session["CaseNo"]);
            command2.ExecuteNonQuery();
            scon.Close();

            for (int i = 0; i < dt.Rows.Count; i++)
            {

                SqlCommand command = new SqlCommand("INSERT INTO [PhysicalEvaluation] (CID,CaseNo,PhysicalEvalCode,PhysicalEvalValue,RecorderID,EvalDateTime,CMPlanNo) VALUES('" + dt.Rows[i]["CID"].ToString() + "','" + dt.Rows[i]["CaseNo"].ToString() + "','" + dt.Rows[i]["PhysicalEvalCode"].ToString() + "','" + dt.Rows[i]["PhysicalEvalValue"].ToString() + "','" + dt.Rows[i]["RecorderID"].ToString() + "','" + dt.Rows[i]["EvalDateTime"].ToString() + "','" + "1" + "')", scon);
                scon.Open();

                command.ExecuteNonQuery();
                scon.Close();
            }
        }
        protected void SelfCareEval()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            if (ds.Columns.Count <= 0)
            {
                ds.Columns.Add("CID");
                ds.Columns.Add("CaseNo");
                ds.Columns.Add("SelfEvalCode");
                ds.Columns.Add("SelfEvalValue");
                ds.Columns.Add("RecorderID");
                ds.Columns.Add("EvalDateTime");
                ds.Columns.Add("CMPlanNo");


            }
            foreach (GridViewRow row in GridView2.Rows)
            {
                scon.Open();
                SqlCommand cmd = new SqlCommand("select SelfEvalCode from SelfCareAbility where Name = @sj", scon);
                cmd.Parameters.AddWithValue("@sj", GridView2.Rows[row.RowIndex].Cells[0].Text);
                RadioButtonList RBLS = (RadioButtonList)row.FindControl("RBL2");
                SqlDataReader reader1 = cmd.ExecuteReader();
                if (reader1.Read() != false)
                {
                    ds.Rows.Add(Session["CID"], Session["CaseNo"], reader1["SelfEvalCode"].ToString(), RBLS.SelectedValue.ToString(), Session["Staff_id"], DateTime.Now.ToString(), Session["CMPlanNo"]);
                }
                else
                {

                }
                scon.Close();
            }
            //GridView4.DataSource = ds;
            //GridView4.DataBind();
            SqlCommand command22 = new SqlCommand("delete from [SelfCareEvaluation] where CID=@CID and CaseNo=@CaseNo", scon);
            scon.Open();
            command22.Parameters.AddWithValue("@CID", Session["CID"]);
            command22.Parameters.AddWithValue("@CaseNo", Session["CaseNo"]);
            command22.ExecuteNonQuery();
            scon.Close();
            for (int i = 0; i < ds.Rows.Count; i++)
            {

                SqlCommand command = new SqlCommand("INSERT INTO [SelfCareEvaluation] (CID,CaseNo,SelfEvalCode,SelfEvalValue,RecorderID,EvalDateTime,CMPlanNo) VALUES('" + ds.Rows[i]["CID"].ToString() + "','" + ds.Rows[i]["CaseNo"].ToString() + "','" + ds.Rows[i]["SelfEvalCode"].ToString() + "','" + ds.Rows[i]["SelfEvalValue"].ToString() + "','" + ds.Rows[i]["RecorderID"].ToString() + "','" + ds.Rows[i]["EvalDateTime"].ToString() + "','" + "1" + "')", scon);
                scon.Open();

                command.ExecuteNonQuery();
                scon.Close();






                SqlCommand command33 = new SqlCommand("INSERT INTO [SelfCareEvaluation_log] (CID,CaseNo,SelfEvalCode,SelfEvalValue,RecorderID,EvalDateTime,CMPlanNo) VALUES('" + ds.Rows[i]["CID"].ToString() + "','" + ds.Rows[i]["CaseNo"].ToString() + "','" + ds.Rows[i]["SelfEvalCode"].ToString() + "','" + ds.Rows[i]["SelfEvalValue"].ToString() + "','" + ds.Rows[i]["RecorderID"].ToString() + "','" + ds.Rows[i]["EvalDateTime"].ToString() + "','" + "1" + "')", scon);
                scon.Open();

                command33.ExecuteNonQuery();
                scon.Close();



            }

          //  crepsnumber();
           // psnumbertolabel();
           // creDetails();



        }












     





        public void BindDataread()
        {



            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);


            scon.Open();
            DataTable dtphy = new DataTable();


            SqlCommand command2 = new SqlCommand("select a.PhysicalEvalValue as PhysicalEvalCode from [PhysicalEvaluation] a join PhysicalProblem b on a.PhysicalEvalCode=b.PhysicalEvalCode where CID=@CID and CaseNo=@CaseNo", scon);

            command2.Parameters.AddWithValue("@CID", Session["CID"]);
            command2.Parameters.AddWithValue("@CaseNo", Session["CaseNo"]);
            command2.ExecuteNonQuery();




            SqlDataReader reader8 = command2.ExecuteReader();

            dtphy.Columns.Add("PhysicalEvalCode");

            while (reader8.Read() != false)
            {
                dtphy.Rows.Add(reader8["PhysicalEvalCode"].ToString());
                //x3.Add(reader8["ACTCode"].ToString());
                //x4.Add(reader8["Note"].ToString());

            }
            reader8.Close();


            for (int j = 0; j < dtphy.Rows.Count; j++)
            {
                string a = dtphy.Rows[j]["PhysicalEvalCode"].ToString();
                foreach (GridViewRow GR in GridView1.Rows)
                {
                    string b = GR.Cells[0].Text.ToString();
                    RadioButtonList chk = (RadioButtonList)GR.FindControl("RBL");

                    if (a == b)
                    {

                        //tb.Text = dtphy.Rows[j]["Note"].ToString();
                        // tb.Text=
                        chk.SelectedValue = dtphy.Rows[j]["PhysicalEvalCode"].ToString();
                    }
                }
            }


            scon.Close();







        }

        protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }













    }
}